package com.station.Activitys.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.station.Activitys.R;
import com.station.Activitys.adapter.base.BaseRecyclerViewAdapter;
import com.station.Activitys.bean.CheckUser;
import com.station.Activitys.bean.ReleaseNews;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 项目名称：基站管理平台 手机app
 * 类描述：发布信息 适配器
 * 创建人：张飞祥
 * 创建时间：2018/9/4 8:59
 * 修改人：张飞祥
 * 修改时间：2018/9/4 8:59
 * 修改备注：
 */
public class ReleaseNewsAdapter extends BaseRecyclerViewAdapter{

    //上下文
    private Context mContext;
    //数据源
    private List<ReleaseNews> mReleaseList = null;
    //选中后的数据
    private List<CheckUser> mAfterList = null;
    //全选 flag
    private boolean misSelectAll;


    /**
     * 构造方法
     * @param mContext
     * @param mReleaseList
     */
    public ReleaseNewsAdapter(Context mContext, List<ReleaseNews> mReleaseList) {
        super(mContext, mReleaseList);
        this.mContext = mContext;
        this.mReleaseList = mReleaseList;
        //初始化 选中后的 数据 集合
        mAfterList = new ArrayList<>();
    }

    @Override
    protected int getLayoutItemResId() {
        return R.layout.item_release_news;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {

        if (viewHolder instanceof ReleaseNewsViewHolder) {
            //TODO 获取信息中心 viewHolder
            final ReleaseNewsViewHolder holder = (ReleaseNewsViewHolder) viewHolder;
            //TODO  将数据保存在itemView的Tag中，以便点击时进行获取
            holder.itemView.setTag(position);

            /**
             * 数据填充
             */
            //当前 数据实体
            ReleaseNews news = mReleaseList.get(position);
            //人员列表 数据源
            final List<CheckUser> mCheckUserList  = news.getUsetInfoList();
            //第 0 个 需要显示标题
            if (position == 0){
                holder.tvItemTitle.setVisibility(View.VISIBLE);
            }
            //班组名
            holder.tvItemName.setText(news.getDepartmentName());

            //人员 adapter
            final ReleasePeopleAdapter mAdapter = new ReleasePeopleAdapter(mContext, mCheckUserList) {
                @Override
                public void setOnItemChecked(CheckUser checkUser,boolean mIsSelectAll,boolean isChecked) {

                    //TODO 处理Item点击选中回调事件
                    if (isChecked) {
                        //选中处理
                        if (!mAfterList.contains(checkUser)) {
                            mAfterList.add(checkUser);
                        }
                    } else {
                        //未选中处理
                        if (mAfterList.contains(checkUser)) {
                            mAfterList.remove(checkUser);
                        }
                    }

                    //TODO 全选 处理
                    if (mIsSelectAll){
                        misSelectAll = true;
                        holder.cbSelectAll.setChecked(true);
                    }else{
                        misSelectAll = false;
                        holder.cbSelectAll.setChecked(false);
                    }
                }
            };
            //这里是设置为网格布局，后边4的参数是列数
            holder.rvItemList.setLayoutManager(new GridLayoutManager(mContext,4));
            //设置 适配器 到控件上
            holder.rvItemList.setAdapter(mAdapter);

            /**
             * 子 adapter 数据 全选 事件 监听
             */
            holder.cbSelectAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //设置 tag
                    misSelectAll = !misSelectAll;
                    //动态设置 选中状态
                    if (misSelectAll) {
                        holder.cbSelectAll.setChecked(true);
                        //选中处理
                        for (CheckUser checkUser : mCheckUserList){
                            if (!mAfterList.contains(checkUser)) {
                                mAfterList.add(checkUser);
                            }
                        }

                    }else {
                        holder.cbSelectAll.setChecked(false);
                        //选中处理
                        for (CheckUser checkUser : mCheckUserList){
                            if (mAfterList.contains(checkUser)) {
                                mAfterList.remove(checkUser);
                            }
                        }
                    }

                    //TODO 全选
                    for (CheckUser user : mCheckUserList) {
                        user.setChecked(misSelectAll);
                    }
                    notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public RecyclerView.ViewHolder getViewHolder(View view) {
        return new ReleaseNewsViewHolder(view);
    }

    /**
     * 返回 当前选择的 人员id 集合
     */
    public String getUserIds(){
        //要返回的 str
        String idStr = "";
        //循环 拼接字符串
        for (int i = 0;i<mAfterList.size();i++){
            if (i == 0)
                idStr += mAfterList.get(i).getId();
            else
                idStr += ","+mAfterList.get(i).getId();
        }
        return idStr;
    }

    /**
     * ViewHolder
     */
    public class ReleaseNewsViewHolder extends RecyclerView.ViewHolder {

        //标题
        @Bind(R.id.tv_item_title)
        TextView tvItemTitle;
        //班组名
        @Bind(R.id.tv_item_name)
        TextView tvItemName;
        //全选
        @Bind(R.id.cb_select_all)
        CheckBox cbSelectAll;
        //人员列表
        @Bind(R.id.rv_item_list)
        RecyclerView rvItemList;

        public ReleaseNewsViewHolder(View itemView) {
            super(itemView);
            //初始化 控件绑定
            ButterKnife.bind(this, itemView);
            //初始化 屏幕适配
            AutoUtils.autoSize(itemView);
        }
    }
}
